package com.game.ui.progress
{
	import com.game.core.res.GameResource;
	import com.game.core.res.Resource;
	import com.game.ui.PopUpManager;
	
	import flash.events.ProgressEvent;

	public class SceProgressBarManager
	{
		private var _loadId : String;
		private var _progressBar : ProgressBar;
		private var _completeNotify : Function;
		private var _resTotal : Number;
		private var _currentLoadByte : Number;
		
		private static var _instance : SceProgressBarManager;
		public static function getInstance():SceProgressBarManager
		{
			if(!_instance)
			{
				_instance = new SceProgressBarManager();
			}
			return _instance;
		}
		
		public function SceProgressBarManager()
		{
			super();
			_resTotal = 0;
			_currentLoadByte = 0;
			_progressBar = new ProgressBar();
		}
		
		public function show():void
		{
			PopUpManager.addPop(_progressBar);
			PopUpManager.centerPopUp(_progressBar);
		}
		
		public function hide():void
		{
			PopUpManager.removePop(_progressBar);
		}
		
		public function reset():void
		{
			_progressBar.reset();
		}
		
		public function setLoadId(id:String):SceProgressBarManager
		{
			_loadId = id;
			return this;
		}
		
		public function load(notify:Function):SceProgressBarManager
		{
			_completeNotify = notify;
			Resource.getResource(_loadId).onComplete(completeNotify).onProgress(progressNofity).load();
			return this;
		}
		
		private function completeNotify(res:GameResource):void
		{
			if(_completeNotify != null)
			{
				var completeNotify:Function = _completeNotify;
				_completeNotify = null;
				completeNotify(res);
			}
		}
		
		private function progressNofity(progress : ProgressEvent):void
		{
			var loadByte : Number = progress.bytesLoaded;
			var loadTotal : Number = progress.bytesTotal;	
//			trace("_currentLoadByte == "+ _currentLoadByte +"   loadByte ==" + loadByte + "  loadTotal == "+loadTotal);
			_progressBar.setPercent(_currentLoadByte + loadByte,_resTotal);
			if(loadByte == loadTotal)
			{
				_currentLoadByte += loadTotal;
				trace("_currentLoadByte == " + _currentLoadByte);
			}
		}
		
		public function setLoaderInfo(value:String):SceProgressBarManager
		{
			_progressBar.setLoaderInfo(value);
			return this;
		}

		/**
		 * 设置批量加载的资源总大小 
		 * @param value Number
		 */		
		public function set resTotal(value:Number):void
		{
			_resTotal = value;
		}

		
	}
}